<?php
namespace app\user\service;
use app\common\library\Service;

class Distributor extends Service
{
    use \app\common\library\traits\Model;

    protected $model = null;
    protected $modelValidate = "user/user_distributor";

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = model('user/user_distributor');
    }

    /**
     * 申请分销商
     * @param $user_id 用户ID
     * @param array $info 申请信息
     *  {
     *      company_name    ： 名称
     *      company_area    ：
     *      company_address ：
     *      company_license ：
     *      company_legal   ：
     *      company_photo   ：
     *      introduce_wechat ：
     * }
     * @return boolean;
     */
    public function apply($user_id, $info = []) {
        $user = model('user/user', 'service')->find($user_id);
        if(!$user) {
            $this->error = '用户不存在';
            return false;
        }
        if($user['role'] !== 0) {
            $this->error = "您当前的角色不允许申请分销商";
            return false;
        }

        $apply = $this->model->where([
            'uid' => $user_id,
            'status' => 0,
        ])->find();

        if($apply) {
            $this->error = '资格正在审核中';
            return false;
        }

        $info['uid'] = $user_id;
        $info['area'] = is_array($info['area']) ? implode(",",$info['area']) : '';
        $info['license_pic'] = (string) $info['license_pic'];
        $info['idcard_pic'] = (string) $info['idcard_pic'];
        $info['works_pic'] = (string) $info['works_pic'];

        if($info['customer_code']) {
            $customer_id = model('user/user', 'service')->where([
                'customer_code' => $info['customer_code'],
                'role' => 2,
                'status' => 1,
            ])->value("id");
            $info['customer_id'] = (int) $customer_id;
        }

        \think\Db::startTrans();
        $result = $this->save($info, [], 'apply');

        if(!$result) {
            \think\Db::rollback();
            $this->error = $this->getError();
            return false;
        }

        \think\Hook::listen('UserDistributorApplyAfter', $result);
        \think\Db::commit();
        return $result;
    }

    /**
     * 分销商审核
     * @param integer $id 申请单ID
     * @param integer $status  审核状态
     * @param string $message string  审核备注
     * @return boolean;
     */
    public function review($id, $status = 1, $message) {
        $apply = $this->model->find($id);
        if(!$apply || $apply['status'] !== 0) {
//            $this->error = "申请单不存在或当前无需审核";
//            return false;
        }

        $apply->review_time = time();
        $apply->status = (int) $status;
        $apply->manage_remark = (string) $message;

        \think\Db::startTrans();
        $result = $apply->save();

        if(!$result) {
            \think\Db::rollback();
            $this->error = $apply->getError();
            return false;
        }
        if($status == 1) {
            $info = [
                'id' => $apply['uid'],
                'role' => 1,
                'customer_id' => (int) $apply['customer_id'],
            ];
            $user_result = model('user/user', 'service')->save($info, ['id' => $apply['uid']], 'review');
            if(!$user_result) {
                \think\Db::rollback();
                $this->error = model('user/user', 'service')->getError();
                return false;
            }
        }

        \think\Hook::listen("UserDistributorReviewAfter", $apply);
        \think\Db::commit();
        return $apply;
    }
}